﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Main.Structure;
using Main.Substructure;

namespace Main.Menus.Submenus
{
    public partial class AddClient : Form
    {
        /// <summary>
        /// Class constructor.
        /// </summary>
        public AddClient()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// This method is called when the "Add Client" button is clicked.
        /// This method creates a new client based off of the form information and then saves it.
        /// </summary>
        private void btnAddClient_Click(object sender, EventArgs e)
        {
            // Make sure the user has filled in all the information that a client has to have.
            #region Empty Data Fields Check
            foreach (Control control in this.Controls)
            {
                if (control is GroupBox)
                    foreach (Control subControl in control.Controls)
                        if (subControl is TextBox)
                            if (subControl.Text == "")
                            {
                                Utilities.Message("You must fill out all data fields before adding a new client.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
            }
            #endregion
            // Create a new client and assign it a new ID that hasn't been used before.
            Client client = new Client((uint)Kernel.Clients.Count + 1);
            client.FirstName = txtBoxFirstName.Text;
            client.LastName = txtBoxLastName.Text;
            client.Street = txtBoxStreet.Text;
            client.City = txtBoxCity.Text;
            client.State = txtBoxState.Text;
            client.Zip = txtBoxZip.Text;
            client.Phone = txtBoxPhone.Text;
            client.Notes = txtBoxNotes.Text;

            if (Database.Clients.Create(client))
            {
                Utilities.Message("Client created and saved successfully.", "Client Created", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Dispose();
            }
            else Utilities.Message("Client could not be created.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// This method is called when the "Cancel" button is clicked.
        /// This method disposes of this form.
        /// </summary>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
